﻿using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using StockholmsStad.Windows.UI.Modules.Maintenance.Models;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Controls
{
    /// <summary>
    /// Interaction logic for GeneratedDaysControl.xaml
    /// </summary>
    public partial class GeneratedDaysControl
    {
        public List<string> StatusTypes = new List<string> {"Aktiv", "Paus"};

        public GeneratedDaysControl()
        {
            InitializeComponent();
        }

        public MinisterTimeBlockPresentationModel Model
        {
            get { return DataContext as MinisterTimeBlockPresentationModel; }
            set { DataContext = value; }
        }

        public List<string> GetStatusTypes()
        {
            return StatusTypes;
        }

        private void OnUncheckItem(object sender, RoutedEventArgs e)
        {
            cbSelectAll.IsChecked = false;
        }

        private void OnSelectAllChanged(object sender, RoutedEventArgs e)
        {
            if (cbSelectAll.IsChecked.HasValue && cbSelectAll.IsChecked.Value)
                lvDates.SelectAll();
            else
            {
                lvDates.UnselectAll();
            }
        }

        private void cbInactive_Checked(object sender, RoutedEventArgs e)
        {
            if (lvDates.SelectedItems != null && lvDates.SelectedItems.Count > 0)
            {
                Toggle_Status(TimeBlockStatus.Break);
            }
            else
            {
                MessageBox.Show("Du måste markera minst ett tidsblock i listan. ", "Information",
                    MessageBoxButton.OK, MessageBoxImage.Information);

            }

            var checkBox = sender as CheckBox;
            if (checkBox != null) checkBox.IsChecked = false;
        }

        private void cbActive_Checked(object sender, RoutedEventArgs e)
        {
            if (lvDates.SelectedItems != null && lvDates.SelectedItems.Count > 0)
            {
                Toggle_Status(TimeBlockStatus.Bookable);
            }
            else
            {
                MessageBox.Show("Du måste markera minst ett tidsblock i listan. ", "Information",
                    MessageBoxButton.OK, MessageBoxImage.Information);

            }
            var checkBox = sender as CheckBox;
            if (checkBox != null) checkBox.IsChecked = false;
        }

        private void Toggle_Status(TimeBlockStatus status)
        {
            for (int i = lvDates.SelectedItems.Count - 1; i >= 0; i--)
            {
                var timeBlock = lvDates.SelectedItems[i] as MinisterTimeBlock;
                if (timeBlock != null) timeBlock.TimeBlockStatus = status;
            }

            lvDates.Items.Refresh();
        }

        private void btnAssign_Click(object sender, RoutedEventArgs e)
        {
            if (lvDates.SelectedItems != null && lvDates.SelectedItems.Count > 0)
            {
                for (int i = lvDates.SelectedItems.Count - 1; i >= 0; i--)
                {
                    var timeBlock = lvDates.SelectedItems[i] as MinisterTimeBlock;
                    if (timeBlock != null)
                    {
                        if (cbTempMinister.IsChecked.HasValue && cbTempMinister.IsChecked.Value)
                        {
                            timeBlock.TempMinister = cbMinisters.SelectionBoxItem as Minister;
                        }
                        else
                        {
                            timeBlock.Minister = cbMinisters.SelectionBoxItem as Minister;
                        }
                    }
                }

                lvDates.Items.Refresh();
            }
            else
            {
                MessageBox.Show("Du måste markera minst ett tidsblock i listan. ", "Information",
                    MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
    }
}
